package com.base.service.system.menu;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.entity.system.Menu;
import com.base.mapper.MenuMapper;
import com.base.util.PageData;

@Service("menuService")
public class MenuServiceImpl implements MenuService {

	@Autowired
	private MenuMapper newMenuMapper;

	// 新增菜单
	public void addMenu(Menu newMenu) throws Exception {
		newMenuMapper.addMenu(newMenu);
	}

	// 根据ID更新菜单
	public void updateMenuById(Menu newMenu) throws Exception {
		newMenuMapper.updateMenuById(newMenu);
	}

	// 根据菜单ID列出它的子菜单
	public List<Menu> listMenuByParentId(int parentId) throws Exception {
		return newMenuMapper.listMenuByParentId(parentId);
	}

	// 根据菜单ID修改菜单信息
	public void editMenuById(Menu menu) throws Exception {
		newMenuMapper.editMenuById(menu);
	}

	// 根据顶级菜单ID修改菜单图片
	public void editMenuIconById(Menu menu) throws Exception {
		newMenuMapper.editMenuIconById(menu);
	}

	// 找到当前最大的MENU的ID
	public Integer findMaxMenuId() throws Exception {
		return newMenuMapper.findMaxMenuId();
	}

	// 根据菜单ID找到该菜单
	public Menu findMenuByMenuId(int menu_id) throws Exception {
		return newMenuMapper.findMenuByMenuId(menu_id);
	}

	public void deleteMenuById(String menuId) throws Exception {
		newMenuMapper.deleteMenuById(menuId);
	}

	public PageData getMenuById(PageData pd) throws Exception {
		return newMenuMapper.getMenuById(pd);
	}

	// 取最大id
	public PageData findMaxId(PageData pd) throws Exception {
		return newMenuMapper.findMaxId(pd);
	}

	public List<Menu> listAllParentMenu() throws Exception {
		return newMenuMapper.listAllParentMenu();
	}

	public void saveMenu(Menu menu) throws Exception {
		if (menu.getMenuId() != null) {
			newMenuMapper.updateMenuById(menu);
		} else {
			newMenuMapper.insertMenu(menu);
		}
	}

	public List<Menu> listSubMenuByParentId(String parentId) throws Exception {
		return newMenuMapper.listSubMenuByParentId(parentId);
	}

	public List<Menu> listAllMenu() throws Exception {
		List<Menu> rl = this.listAllParentMenu();
		for (Menu menu : rl) {
			List<Menu> subList = this.listSubMenuByParentId(String.valueOf(menu.getMenuId()));
			menu.setSubMenu(subList);
		}
		return rl;
	}

	public List<Menu> listAllSubMenu() throws Exception {
		return newMenuMapper.listAllSubMenu();
	}

	/**
	 * 编辑
	 */
	public PageData edit(PageData pd) throws Exception {
		return newMenuMapper.updateMenu(pd);
	}

	/**
	 * 保存菜单图标 (顶部菜单)
	 */
	public PageData editicon(PageData pd) throws Exception {
		return newMenuMapper.editicon(pd);
	}

	/**
	 * 更新子菜单类型菜单
	 */
	public PageData editType(PageData pd) throws Exception {
		return newMenuMapper.editType(pd);
	}
}
